<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户编号" prop="customerNo">
        <el-input
          v-model="queryParams.customerNo"
          placeholder="请输入客户编号"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="customerContact">
        <el-input
          v-model="queryParams.customerContact"
          placeholder="请输入联系人"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="customerContact">
        <el-input
          v-model="queryParams.customerPhone"
          placeholder="请输入联系人电话"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="customerCompany">
        <el-input
          v-model="queryParams.customerCompany"
          placeholder="请输入公司名称"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务次数" prop="customerCompany">
        <el-input
          v-model="queryParams.customerExperience"
          placeholder="请输入业务次数"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型">
        <el-select v-model="queryParams.customerTypeId"
                   placeholder="请选择"
                   style="width: 240px"
                   size='mini'>
          <el-option v-for="dict in dict.type.ipms_customer_type"
                     :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery(queryParams)">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery(queryParams)">重置</el-button>
      </el-form-item>

      <el-form-item>
        <el-col :span="1.5">
          <el-button type="primary"
                     plain
                     icon="el-icon-remove"
                     size="mini"
                     @click="handleUpdate">批量移回公海</el-button>
        </el-col>
      </el-form-item>
    </el-form>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <el-table :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编号" align="center" prop="customerNo" />
      <el-table-column label="客户渠道" align="center" prop="customerFrom"  :formatter="formatFrom" />
      <el-table-column label="联系人" align="center" prop="customerContact" />
      <el-table-column label="联系人电话" align="center" prop="customerPhone" />
      <el-table-column label="联系人邮箱" align="center" prop="customerEmail" />
      <el-table-column label="公司名称" align="center" prop="customerCompany" />
      <el-table-column label="客户单位类型" align="center" prop="customerTypeId" :formatter="formatType"/>
      <el-table-column label="公司地址" align="center" prop="customerAddress" />
      <el-table-column label="客户业务次数" align="center" prop="customerExperience" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-remove"
            @click="handleUpdate(scope.row)"
          >移回公海</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { getRecycleCustomer,updateRecycleCustomer } from "@/api/crm/crm";

export default {
  dicts:["ipms_customer_from","ipms_customer_type"],
  name: "Customer",
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerNo: null,
        customerContact: null,
        customerPhone: null,
        customerCompany: null,
        customerExperience: null,
        customerTypeId: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //格式化字典参数
    formatFrom(row){
      return this.selectDictLabel(this.dict.type.ipms_customer_from,row.customerFromId)
    },
    formatType(row){
      return this.selectDictLabel(this.dict.type.ipms_customer_type,row.customerTypeId)
    },
    /** 查询客户列表 */
    getList() {
      getRecycleCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.customerNo = null;
      this.queryParams.customerContact = null;
      this.queryParams.customerPhone = null;
      this.queryParams.customerCompany = null;
      this.queryParams.customerExperience = null;
      this.queryParams.customerTypeId = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 移回公海按钮操作 */
    handleUpdate(row) {
      const customerIds = row.customerId || this.ids
      updateRecycleCustomer(customerIds).then(response => {
        this.$message.success('客户移回公海成功');
        this.handleQuery();
      });
    },
  }
};
</script>

